package com.imooc.bilibili.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.imooc.bilibili.dao.DanmuDao;
import com.imooc.bilibili.domain.Danmu;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class DanmuService {

    @Autowired
    private DanmuDao danmuDao;

    @Autowired
    private StringRedisTemplate redisTemplate;

    public static final String DANMU_KEY = "danmu-video-";

    /**
     * 添加弹幕
     * @param danmu
     * @return
     */
    public void addDanmu(Danmu danmu){
        danmuDao.addDanmu(danmu);
    }

    @Async
    public void asyncAddDanmu(Danmu danmu){
        danmuDao.addDanmu(danmu);
    }

    /**
     * 获取弹幕列表
     * 优先查询redis中的数据，如果没有再查mysql并添加到redis
     * @param
     * @return
     */
    public List<Danmu> getDanmus(Long videoId, String startTime, String endTime) throws ParseException {
        String key = DANMU_KEY + videoId;
        String value = redisTemplate.opsForValue().get(key);
        List<Danmu> danmuList;
        if (!StringUtils.isNullOrEmpty(value)){
            danmuList = JSONArray.parseArray(value, Danmu.class);
            if (!StringUtils.isNullOrEmpty(startTime) && !StringUtils.isNullOrEmpty(endTime)){
                DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                Date startDate = sdf.parse(startTime);
                Date endDate = sdf.parse(endTime);
                List<Danmu> childList = new ArrayList<>();
                for (Danmu danmu : danmuList){
                    Date createTime = danmu.getCreateTime();
                    if (createTime.after(startDate) && createTime.before(endDate)){
                        childList.add(danmu);
                    }
                }
                danmuList = childList;
            }
        }else {
            Map<String, Object> pararms = new HashMap<>();
            pararms.put("videoId",videoId);
            pararms.put("startTime",startTime);
            pararms.put("endTime",endTime);
            danmuList = danmuDao.getDanmus(pararms);
            // 保存到redis
            redisTemplate.opsForValue().set(DANMU_KEY,JSONArray.toJSONString(danmuList));
        }
        return danmuList;
    }

    /**
     * 添加弹幕到redis
     * @param danmu
     */
    public void addDanmusToRedis(Danmu danmu){
        String key = DANMU_KEY + danmu.getVideoId();
        String value = redisTemplate.opsForValue().get(key);
        List<Danmu> list = new ArrayList<>();
        if (!StringUtils.isNullOrEmpty(value)){
            list = JSONArray.parseArray(value,Danmu.class);
        }
        list.add(danmu);
        redisTemplate.opsForValue().set(key, JSONObject.toJSONString(list));
    }


}
